home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / datamgmt / example-1.1 / AppletButton.java next >
Encoding:
Java Source  |  1997-07-13  |  3.7 KB  |  118 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.util.*;
  19. import java.applet.Applet;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.ActionEvent;
  22.  
  23. public class AppletButton extends Applet implements Runnable, ActionListener {
  24.     int frameNumber = 1;
  25.     String buttonText = "Click here to bring up AroundTheWorld";
  26.     String windowTitle = "AroundTheWorld";
  27.     int requestedWidth = 0;
  28.     int requestedHeight = 0;
  29.     Button button;
  30.     Thread windowThread;
  31.     Label label;
  32.     boolean pleaseCreate = false;
  33.  
  34.     public void init() {
  35.  
  36.         String windowWidthString = getParameter("WINDOWWIDTH");
  37.         if (windowWidthString != null) {
  38.             try {
  39.                 requestedWidth = Integer.parseInt(windowWidthString);
  40.             } catch (NumberFormatException e) {
  41.                 //Use default width.
  42.             }
  43.         }
  44.  
  45.         String windowHeightString = getParameter("WINDOWHEIGHT");
  46.         if (windowHeightString != null) {
  47.             try {
  48.                 requestedHeight = Integer.parseInt(windowHeightString);
  49.             } catch (NumberFormatException e) {
  50.                 //Use default height.
  51.             }
  52.         }
  53.  
  54.         setLayout(new GridLayout(2,0));
  55.         add(button = new Button(buttonText));
  56.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  57.  
  58.         add(label = new Label("", Label.CENTER));
  59.     button.addActionListener(this);
  60.     }
  61.  
  62.     public void start() {
  63.         if (windowThread == null) {
  64.             windowThread = new Thread(this, "Bringing Up AroundTheWorld");
  65.             windowThread.start();
  66.         }
  67.     }
  68.  
  69.     public synchronized void run() {
  70.         while (windowThread != null) {
  71.             while (pleaseCreate == false) {
  72.                 try {
  73.                     wait();
  74.                 } catch (InterruptedException e) {
  75.                 }
  76.             }
  77.  
  78.             //We've been asked to bring up a window.
  79.             pleaseCreate = false;
  80.             Frame window = new IntlWindow(this);
  81.  
  82.         if (frameNumber == 1) {
  83.             window.setTitle(windowTitle);
  84.         } else {
  85.             window.setTitle(windowTitle + ": " + frameNumber);
  86.         }
  87.         frameNumber++;
  88.  
  89.         //Set the window's size.
  90.         window.pack();
  91.         if ((requestedWidth > 0) | (requestedHeight > 0)) {
  92.             window.setSize(Math.max(requestedWidth,
  93.                    window.getSize().width),
  94.                    Math.max(requestedHeight,
  95.                    window.getSize().height));
  96.         }
  97.  
  98.         window.show();
  99.         label.setText("");
  100.     }
  101.     }
  102.                 
  103.     public synchronized void actionPerformed(ActionEvent event) {
  104.     Dimension d = null;
  105.     try {
  106.         d = getSize(); //1.1 code
  107.             if (event.getSource() instanceof Button) {
  108.                 //signal the window thread to build a window
  109.                 label.setText("Please wait while the window comes up...");
  110.                 pleaseCreate = true;
  111.                 notify();
  112.             } 
  113.     } catch (NoSuchMethodError e) {
  114.         label.setText("Your browser can't run 1.1 applets.");
  115.     }
  116.     }
  117. }
  118.